<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/atom.xml" title="Likianta Blog" type="application/atom+xml" />






<meta name="description" content="本文将介绍通过正则表达式获得纯净的原始字幕（txt文本），并提取其中的加星文字作为进一步处理的源文本。">
<meta property="og:type" content="article">
<meta property="og:title" content="使用正则表达式匹配Aegisub字幕（实战篇）">
<meta property="og:url" content="https://likianta.coding.me/2018/0116-201908/index.html">
<meta property="og:site_name" content="Likianta Blog">
<meta property="og:description" content="本文将介绍通过正则表达式获得纯净的原始字幕（txt文本），并提取其中的加星文字作为进一步处理的源文本。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/48017464.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/94722208.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/36142220.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/98366587.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/21035932.jpg">
<meta property="og:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/46995228.jpg">
<meta property="og:updated_time" content="2018-01-22T11:47:01.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用正则表达式匹配Aegisub字幕（实战篇）">
<meta name="twitter:description" content="本文将介绍通过正则表达式获得纯净的原始字幕（txt文本），并提取其中的加星文字作为进一步处理的源文本。">
<meta name="twitter:image" content="http://ozurciydg.bkt.clouddn.com/18-1-16/48017464.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://likianta.coding.me/2018/0116-201908/"/>





  <title>使用正则表达式匹配Aegisub字幕（实战篇） | Likianta Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Likianta Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tutorials">
          <a href="/tags/tutorial-catalog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            教程
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://likianta.coding.me/2018/0116-201908/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Likianta">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Likianta Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">使用正则表达式匹配Aegisub字幕（实战篇）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-16T20:19:08+08:00">
                2018-01-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/fansub/" itemprop="url" rel="index">
                    <span itemprop="name">字幕组</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,584
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  11
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文将介绍通过正则表达式获得纯净的原始字幕（txt文本），并提取其中的加星文字作为进一步处理的源文本。</p>
<a id="more"></a>
<h1 id="预备"><a href="#预备" class="headerlink" title="预备"></a>预备</h1><p>本次实战需要处理的原始字幕文本：《一披萨，一世界》（下载：链接: <a href="https://pan.baidu.com/s/1brffdnT" target="_blank" rel="noopener">https://pan.baidu.com/s/1brffdnT</a> 密码: teyb）</p>
<p>全文如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">[Script Info]</span><br><span class="line">Title:</span><br><span class="line">Original Script:</span><br><span class="line">Original Translation:</span><br><span class="line">Original Editing:</span><br><span class="line">Original Timing:</span><br><span class="line">Original Script Checking:</span><br><span class="line">ScriptType: v4.00+</span><br><span class="line">Collisions: Normal</span><br><span class="line">Timer: 100.0000</span><br><span class="line">WrapStyle: 0</span><br><span class="line">ScaledBorderAndShadow: Yes</span><br><span class="line"></span><br><span class="line">[V4+ Styles]</span><br><span class="line">Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding</span><br><span class="line">Style: Default,Arial,20,&amp;H00FFFFFF,&amp;H000000FF,&amp;H00000000,&amp;H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1</span><br><span class="line">Style: 封面白粗,Heiti SC,30,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00FFFFFF,0,0,0,0,100,100,0,0,1,0,0,2,10,10,10,1</span><br><span class="line">Style: 封面黑粗,Heiti SC,30,&amp;H00000000,&amp;H00000000,&amp;H00000000,&amp;H00000000,0,0,0,0,100,100,0,0,1,0.6,0,2,10,10,10,1</span><br><span class="line">Style: 白色字体,Heiti SC,26,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00000000,0,0,0,0,100,100,0,0,1,0.4,0.5,2,10,10,10,1</span><br><span class="line">Style: 水印,Heiti SC,16,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00000000,0,0,0,0,100,100,0,0,1,0.1,0.1,7,10,10,10,1</span><br><span class="line">Style: 注释,Heiti SC,13,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00FFFFFF,&amp;H00000000,0,0,0,0,100,100,0,0,1,0.1,0.1,8,10,10,10,1</span><br><span class="line"></span><br><span class="line">[Events]</span><br><span class="line">Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text</span><br><span class="line">Dialogue: 0,0:00:01.67,0:00:04.85,封面白粗,,0000,0000,0000,,吃遍*天下*美食 尽在一块披萨</span><br><span class="line">Dialogue: 0,0:00:05.63,0:00:08.52,封面白粗,,0000,0000,0000,,*世界美食*披萨店位于洛杉矶*\N12号塔楼*</span><br><span class="line">Dialogue: 0,0:00:09.12,0:00:11.36,封面白粗,,0000,0000,0000,,从*泰国*风味开始</span><br><span class="line">Dialogue: 0,0:00:15.73,0:00:18.53,封面白粗,,0000,0000,0000,,用*青椰咖喱*酱铺底</span><br><span class="line">Dialogue: 0,0:00:19.09,0:00:21.37,封面白粗,,0000,0000,0000,,加入鲜*虾*和*花生*</span><br><span class="line">Dialogue: 0,0:00:24.24,0:00:26.35,封面白粗,,0000,0000,0000,,用*辣虾*盖在面饼上</span><br><span class="line">Dialogue: 0,0:00:26.69,0:00:29.25,封面白粗,,0000,0000,0000,,然后是*意式*风味</span><br><span class="line">Dialogue: 0,0:00:32.64,0:00:35.89,封面白粗,,0000,0000,0000,,在*蕃茄酱*上放些*肉丸*</span><br><span class="line">Dialogue: 0,0:00:38.79,0:00:42.41,封面白粗,,0000,0000,0000,,用*罗勒叶*和*马苏里拉奶酪*\N点缀完工</span><br><span class="line">Dialogue: 0,0:00:43.44,0:00:45.87,封面白粗,,0000,0000,0000,,接着是*英式*风味</span><br><span class="line">Dialogue: 0,0:00:48.52,0:00:50.62,封面白粗,,0000,0000,0000,,用*香肠* *熏肉*和*豌豆*</span><br><span class="line">Dialogue: 0,0:00:52.06,0:00:54.21,封面白粗,,0000,0000,0000,,在披萨上营造*英式早餐*元素</span><br><span class="line">Dialogue: 0,0:00:57.03,0:00:58.93,封面白粗,,0000,0000,0000,,最后是*美式*风味</span><br><span class="line">Dialogue: 0,0:00:59.73,0:01:01.86,封面白粗,,0000,0000,0000,,添加*菲力乳酪牛排*馅</span><br><span class="line">Dialogue: 0,0:01:03.69,0:01:05.56,封面白粗,,0000,0000,0000,,淋上*奶酪酱*</span><br><span class="line">Dialogue: 0,0:01:06.03,0:01:09.03,封面白粗,,0000,0000,0000,,额外洒些*奶酪丝*</span><br><span class="line">Dialogue: 0,0:01:09.46,0:01:12.05,白色字体,,0000,0000,0000,,进炉烤制！</span><br><span class="line">Dialogue: 0,0:01:17.36,0:01:19.89,白色字体,,0000,0000,0000,,在披萨上最后润色一下</span><br><span class="line">Dialogue: 0,0:01:25.68,0:01:28.36,封面白粗,,0000,0000,0000,,真是美食与味蕾的美丽*邂逅*！</span><br></pre></td></tr></table></figure>
<h1 id="需求与目标"><a href="#需求与目标" class="headerlink" title="需求与目标"></a>需求与目标</h1><ol>
<li>获得纯净的文本（本文内容）</li>
<li>将加星的词语改为带颜色的字体（利用Java实现）</li>
<li>合成富文本文档（利用Android app实现）</li>
</ol>
<p>其中第一条是本文要实现的内容。</p>
<p>下面开始实现：</p>
<hr>
<h1 id="实战匹配"><a href="#实战匹配" class="headerlink" title="实战匹配"></a>实战匹配</h1><h2 id="1-将开头的这段文字删去"><a href="#1-将开头的这段文字删去" class="headerlink" title="1. 将开头的这段文字删去"></a>1. 将开头的这段文字删去</h2><p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/48017464.jpg" alt=""></p>
<p><strong>匹配式：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(.*\n)*\b.*Text\b</span><br></pre></td></tr></table></figure>
<p><strong>匹配效果：</strong></p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/94722208.jpg" alt=""></p>
<p><strong>匹配详解：</strong></p>
<ol>
<li>开头的点号用于匹配（除换行符外的）<strong>任意字符</strong></li>
<li><code>\n</code>表示匹配换行符。<code>.</code>和<code>\n</code>组合起来就表示匹配<strong>一段文本</strong></li>
<li>将它们括起来，后面加星号<code>*</code>，星号表示前面的内容出现n次，也就是说三者组合表示匹配<strong>多段文本</strong>（其实就是全文了）</li>
<li>接着用两个<code>\b</code>包裹住<code>.*Text</code>，其中<code>\b</code>是限定符，表示匹配特定的含有“Text”的段落，也就是上图中的最后一段文本</li>
<li>结合3、4两条规则，我们做到了“匹配全文” + “匹配含有‘Text’的段落” = <strong>“从开头直到匹配到含有‘Text’的段落为止”</strong></li>
<li>这样正则匹配就能把图中文字全部匹配到了</li>
</ol>
<p><strong>获得结果：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Dialogue: 0,0:00:01.67,0:00:04.85,封面白粗,,0000,0000,0000,,吃遍*天下*美食 尽在一块披萨</span><br><span class="line">Dialogue: 0,0:00:05.63,0:00:08.52,封面白粗,,0000,0000,0000,,*世界美食*披萨店位于洛杉矶*\N12号塔楼*</span><br><span class="line">Dialogue: 0,0:00:09.12,0:00:11.36,封面白粗,,0000,0000,0000,,从*泰国*风味开始</span><br><span class="line">Dialogue: 0,0:00:15.73,0:00:18.53,封面白粗,,0000,0000,0000,,用*青椰咖喱*酱铺底</span><br><span class="line">Dialogue: 0,0:00:19.09,0:00:21.37,封面白粗,,0000,0000,0000,,加入鲜*虾*和*花生*</span><br><span class="line">Dialogue: 0,0:00:24.24,0:00:26.35,封面白粗,,0000,0000,0000,,用*辣虾*盖在面饼上</span><br><span class="line">Dialogue: 0,0:00:26.69,0:00:29.25,封面白粗,,0000,0000,0000,,然后是*意式*风味</span><br><span class="line">Dialogue: 0,0:00:32.64,0:00:35.89,封面白粗,,0000,0000,0000,,在*蕃茄酱*上放些*肉丸*</span><br><span class="line">Dialogue: 0,0:00:38.79,0:00:42.41,封面白粗,,0000,0000,0000,,用*罗勒叶*和*马苏里拉奶酪*\N点缀完工</span><br><span class="line">Dialogue: 0,0:00:43.44,0:00:45.87,封面白粗,,0000,0000,0000,,接着是*英式*风味</span><br><span class="line">Dialogue: 0,0:00:48.52,0:00:50.62,封面白粗,,0000,0000,0000,,用*香肠* *熏肉*和*豌豆*</span><br><span class="line">Dialogue: 0,0:00:52.06,0:00:54.21,封面白粗,,0000,0000,0000,,在披萨上营造*英式早餐*元素</span><br><span class="line">Dialogue: 0,0:00:57.03,0:00:58.93,封面白粗,,0000,0000,0000,,最后是*美式*风味</span><br><span class="line">Dialogue: 0,0:00:59.73,0:01:01.86,封面白粗,,0000,0000,0000,,添加*菲力乳酪牛排*馅</span><br><span class="line">Dialogue: 0,0:01:03.69,0:01:05.56,封面白粗,,0000,0000,0000,,淋上*奶酪酱*</span><br><span class="line">Dialogue: 0,0:01:06.03,0:01:09.03,封面白粗,,0000,0000,0000,,额外洒些*奶酪丝*</span><br><span class="line">Dialogue: 0,0:01:09.46,0:01:12.05,白色字体,,0000,0000,0000,,进炉烤制！</span><br><span class="line">Dialogue: 0,0:01:17.36,0:01:19.89,白色字体,,0000,0000,0000,,在披萨上最后润色一下</span><br><span class="line">Dialogue: 0,0:01:25.68,0:01:28.36,封面白粗,,0000,0000,0000,,真是美食与味蕾的美丽*邂逅*！</span><br></pre></td></tr></table></figure>
<h2 id="2-删除无关的内容，只留下字幕正文"><a href="#2-删除无关的内容，只留下字幕正文" class="headerlink" title="2. 删除无关的内容，只留下字幕正文"></a>2. 删除无关的内容，只留下字幕正文</h2><p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/36142220.jpg" alt=""></p>
<p><strong>匹配式：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\b.*\b,+</span><br></pre></td></tr></table></figure>
<p><strong>匹配效果：</strong></p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/98366587.jpg" alt=""></p>
<p><strong>匹配详解：</strong></p>
<ol>
<li>两端的<code>\b</code>表示位置限定，也就是说“限定于查找位置限定中的内容”</li>
<li><code>.*</code>用于匹配图中的“Dialogue: 0,0:00:01.67,0:00:04.85,封面白粗,,0000,0000,0000”字段</li>
<li><code>,+</code>用于匹配一个或多个英文逗号，主要用于匹配图中的双逗号“,,”</li>
</ol>
<p><strong>获得（正文字幕）：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">吃遍*天下*美食 尽在一块披萨</span><br><span class="line">*世界美食*披萨店位于洛杉矶*\N12号塔楼*</span><br><span class="line">从*泰国*风味开始</span><br><span class="line">用*青椰咖喱*酱铺底</span><br><span class="line">加入鲜*虾*和*花生*</span><br><span class="line">用*辣虾*盖在面饼上</span><br><span class="line">然后是*意式*风味</span><br><span class="line">在*蕃茄酱*上放些*肉丸*</span><br><span class="line">用*罗勒叶*和*马苏里拉奶酪*\N点缀完工</span><br><span class="line">接着是*英式*风味</span><br><span class="line">用*香肠* *熏肉*和*豌豆*</span><br><span class="line">在披萨上营造*英式早餐*元素</span><br><span class="line">最后是*美式*风味</span><br><span class="line">添加*菲力乳酪牛排*馅</span><br><span class="line">淋上*奶酪酱*</span><br><span class="line">额外洒些*奶酪丝*</span><br><span class="line">进炉烤制！</span><br><span class="line">在披萨上最后润色一下</span><br><span class="line">真是美食与味蕾的美丽*邂逅*！</span><br></pre></td></tr></table></figure>
<h2 id="3-抽取正文中的加星词语"><a href="#3-抽取正文中的加星词语" class="headerlink" title="3. 抽取正文中的加星词语"></a>3. 抽取正文中的加星词语</h2><p>上文中的“*罗勒叶*”、“*奶酪丝*”等加星词语需要改变字体颜色，所以我们要把它们单独抽取出来以备后用。</p>
<p><strong>匹配式：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\*[^*]*\*</span><br></pre></td></tr></table></figure>
<p><strong>匹配效果：</strong></p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/21035932.jpg" alt=""></p>
<p><strong>匹配详解：</strong></p>
<ol>
<li><code>\*</code>是转义字符，表示匹配一个星号符号“*”</li>
<li><code>[^*]</code>是排除法则，用于排除带有星号的词语，如果不使用该法则，“*世界美食*披萨店位于洛杉矶*\N12号塔楼*”会被识别为一个词语“世界美食*披萨店位于洛杉矶*\N12号塔楼”；使用该法则，则会被识别为两个词语：“*世界美食*”和“*\N12号塔楼*”</li>
</ol>
<p><strong>获得：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">*天下*</span><br><span class="line">*世界美食*</span><br><span class="line">*\N12号塔楼*</span><br><span class="line">*泰国*</span><br><span class="line">*青椰咖喱*</span><br><span class="line">*虾*</span><br><span class="line">*花生*</span><br><span class="line">*辣虾*</span><br><span class="line">*意式*</span><br><span class="line">*蕃茄酱*</span><br><span class="line">*肉丸*</span><br><span class="line">*罗勒叶*</span><br><span class="line">*马苏里拉奶酪*</span><br><span class="line">*英式*</span><br><span class="line">*香肠*</span><br><span class="line">*熏肉*</span><br><span class="line">*豌豆*</span><br><span class="line">*英式早餐*</span><br><span class="line">*美式*</span><br><span class="line">*菲力乳酪牛排*</span><br><span class="line">*奶酪酱*</span><br><span class="line">*奶酪丝*</span><br><span class="line">*邂逅*</span><br></pre></td></tr></table></figure>
<h2 id="4-将加星词语的星号以及换行符去除，获得纯净的词语"><a href="#4-将加星词语的星号以及换行符去除，获得纯净的词语" class="headerlink" title="4. 将加星词语的星号以及换行符去除，获得纯净的词语"></a>4. 将加星词语的星号以及换行符去除，获得纯净的词语</h2><p>上文中的星号“*”、“\N”还没有去除，所以现在要把它们除掉。</p>
<p><strong>匹配式：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[*N\\]</span><br></pre></td></tr></table></figure>
<p><strong>匹配效果：</strong></p>
<p><img src="http://ozurciydg.bkt.clouddn.com/18-1-16/46995228.jpg" alt=""></p>
<p><strong>匹配详解：</strong></p>
<ol>
<li>中括号表示查找匹配</li>
<li>中括号内分别表示查找匹配以下三类情况：星号“*”、大写字母“N”以及反斜杠“\”</li>
<li>其中星号无需转义字符，反斜杠必须用转义字符（具体原因不太清楚）</li>
</ol>
<p><strong>获得：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">天下</span><br><span class="line">世界美食</span><br><span class="line">12号塔楼</span><br><span class="line">泰国</span><br><span class="line">青椰咖喱</span><br><span class="line">虾</span><br><span class="line">花生</span><br><span class="line">辣虾</span><br><span class="line">意式</span><br><span class="line">蕃茄酱</span><br><span class="line">肉丸</span><br><span class="line">罗勒叶</span><br><span class="line">马苏里拉奶酪</span><br><span class="line">英式</span><br><span class="line">香肠</span><br><span class="line">熏肉</span><br><span class="line">豌豆</span><br><span class="line">英式早餐</span><br><span class="line">美式</span><br><span class="line">菲力乳酪牛排</span><br><span class="line">奶酪酱</span><br><span class="line">奶酪丝</span><br><span class="line">邂逅</span><br></pre></td></tr></table></figure>
<p>接下来是改变这些文字的颜色，这个任务就只能交给java来处理了。具体可以看下面这篇文章：</p>
<p><a href="">Android 利用SpannableString制作富文本</a></p>
<hr>
<h1 id="相关参考"><a href="#相关参考" class="headerlink" title="相关参考"></a>相关参考</h1><p>正则表达式30分钟入门教程 - Barret Lee - 博客园<br><a href="http://www.cnblogs.com/hustskyking/archive/2013/06/04/RegExp.html" target="_blank" rel="noopener">http://www.cnblogs.com/hustskyking/archive/2013/06/04/RegExp.html</a></p>
<p>正则表达式 – 语法 | 菜鸟教程<br><a href="http://www.runoob.com/regexp/regexp-syntax.html" target="_blank" rel="noopener">http://www.runoob.com/regexp/regexp-syntax.html</a></p>
<p>正则表达式测试工具/常用正则表达式/正则代码生成 - 在线工具<br><a href="https://tool.lu/regex/" target="_blank" rel="noopener">https://tool.lu/regex/</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/0201-171502/" rel="next" title="正则表达式的常用元字符">
                <i class="fa fa-chevron-left"></i> 正则表达式的常用元字符
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/0122193915/" rel="prev" title="关于字重的说明">
                关于字重的说明 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNDI4MS8xMDgxOA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://ozurciydg.bkt.clouddn.com/18-2-12/72826549.jpg"
                alt="Likianta" />
            
              <p class="site-author-name" itemprop="name">Likianta</p>
              <p class="site-description motion-element" itemprop="description">Android Dev</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">73</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="http://blog.csdn.net/likianta" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-crosshairs"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:sheerish@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/likianta" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5441767757/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#预备"><span class="nav-text">预备</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#需求与目标"><span class="nav-text">需求与目标</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#实战匹配"><span class="nav-text">实战匹配</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-将开头的这段文字删去"><span class="nav-text">1. 将开头的这段文字删去</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-删除无关的内容，只留下字幕正文"><span class="nav-text">2. 删除无关的内容，只留下字幕正文</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-抽取正文中的加星词语"><span class="nav-text">3. 抽取正文中的加星词语</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-将加星词语的星号以及换行符去除，获得纯净的词语"><span class="nav-text">4. 将加星词语的星号以及换行符去除，获得纯净的词语</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关参考"><span class="nav-text">相关参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Likianta</span>

  
</div>


  <div class="powered-by">
    <i class="fa fa-user-md"></i>
    <span id="busuanzi_container_site_pv">
      Site visitors:<span id="busuanzi_value_site_pv"></span>
    </span>
  </div>

<!--
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.3</div>

-->



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">Site words total count:75.2k</span>
</div>
        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Evernote";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  

</body>
</html>
